<?php
/**
 * 首页
 */

namespace app\official\controller;


use app\official\model\House as HouseModel;
use app\official\model\Metro as MetroModel;
use app\common\controller\Official;
use app\official\model\HotCity;
use app\official\model\House;
use app\official\model\BusinessArea as BusinessAreaModel;
use app\official\model\Region;
use custom\Redis;
use think\Db;
use think\Exception;
use think\facade\Session;
use think\Request;

class Index extends Official {

    /**
     * 首页房源
     */
    public function index() {
        try {
            $params = $this->checkParams('city_code');
            if ($params === false) {
                return $this->resErr();
            }
            $params['page'] = 1;
            $params['long'] = 12;
            $params['type'] = 'good';
            House::IndexHouseList($params, $good, $pageNum, $count, $page);//热门房源
            $params['type'] = 'new';
            House::IndexHouseList($params, $new, $pageNum, $count, $page);//新增好房
            $hasBusinessArea = BusinessAreaModel::has_data($params);//是否有商圈
            $hasMetro = MetroModel::has_data($params);//是否有地铁
            $wetChatCode = db('company')->where('id', 2)->value('auto_house_url');//经纪人智能选房页面
            return $this->resData('success', compact('good', 'new', 'hasBusinessArea', 'hasMetro', 'wetChatCode'));
        } catch (Exception $e) {
            return $this->resErr($e->getMessage(), $e->getCode());
        }
    }

    /***
     * 首页定位
     * @return false|string
     */
    public function location() {
        try {
            $params = $this->checkParams('');
            if ($params === false) {
                return $this->resErr();
            }
            $hotCity = HotCity::getList();//热门城市
            $treelist = Region::indexCity();//省市结构
            $province = Region::province();//下拉省列表
            return $this->resData('success', compact('hotCity', 'treelist', 'province'));
        } catch (Exception $e) {
            return $this->resErr($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 省列表获取城市
     * @return false|string
     */
    public function getCity() {
        try {
            $params = $this->checkParams('id');
            if ($params === false) {
                return $this->resErr();
            }
            $province = Region::getCity($params['id']);//城市
            return $this->resData('success', $province);
        } catch (Exception $e) {
            return $this->resErr($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 搜索城市
     * @return false|string
     */
    public function searchCity() {
        try {
            $params = $this->checkParams('name');
            if ($params === false) {
                return $this->resErr();
            }
            $city = Region::getCityname($params['name']);//城市
            if (!$city) {
                return $this->resErr();
            } else {
                return $this->resData('success', $city);
            }
        } catch (Exception $e) {
            return $this->resErr($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 名称搜索
     * @return false|string
     */
    public function searchHouse() {
        try {
            $params = $this->checkParams('title,city_code');
            if ($params === false) {
                return $this->resErr();
            }
            $data = HouseModel::searchHouse($params);
            return $this->resData('success', $data);
        } catch (Exception $e) {
            return $this->resErr($e->getMessage(), $e->getCode());
        }
    }

    /**
     * code转经纬度
     * @return false|string
     */
    public function codeToLocation() {
        try {
            $params = $this->checkParams('city_code');
            if ($params === false) return $this->resErr();
            $data = Region::codeToLocation($params['city_code']);
            return $this->resData('success', $data);
        } catch (Exception $e) {
            return $this->resErr($e->getMessage(), $e->getCode());
        }
    }

    function index2(){
        echo "这是index2";
    }
    function login(){
        echo "这是login";
        Session::set('id',1);
    }
    function f(){
        echo "这是f";
    }

}
